/*
 * RemoveExtensionTask.java
 *
 * Copyright (C) 2011 JoomBoss ( http://joomboss.com )
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 */
package com.joomboss;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
 * Ant task implementation for Joomla extension uninstallation
 * @author Aliaksei Kanstantsinau  http://joomboss.com
 * @since March 2011
 * @version 0.1
 *
 * This ant task has following attributes:
 * url - URL to Joomla site (required)
 * name - Extension name (exactly the same as it displayed in Extension Manager) (required)
 * type - Extension type - can be component, module, plugin or template. Default is component. 
 * adminLogin - Joomla Administrator's login (required)
 * adminPassword - Joomla Administrator's password (required)
 * ftpLogin - Site FTP login. Required on most unix systems to be able to perform file operations
 * ftpPassword - Site FTP password. 
 */
public class RemoveExtensionTask extends Task {
	public void execute() {
		if (url == null) {
			throw new IllegalArgumentException("baseUrl should be defined");
		}
		if (adminLogin == null) {
			throw new IllegalArgumentException("adminLogin should be defined");
		}
		if (adminPassword == null) {
			throw new IllegalArgumentException(
					"adminPassword should be defined");
		}
		if (name == null) {
			throw new IllegalArgumentException(
					"Extension name should be defined");
		}

		IJoomlaClient client = getClient(version);
		if(client != null){
			try {
				client.deleteComponent(url, name, type, adminLogin,
						adminPassword, ftpLogin, ftpPassword);
			} catch (Exception e) {
				throw new BuildException(e);
			}
		}
	}
	private IJoomlaClient getClient(String version){
		IJoomlaClient client = null;
		if("1.5".equals(version)){
			client = new JoomlaClient();
		}else if("2.5".equals(version)){
			client = new JoomlaClient2();
		}
		return client;
	}
	
	public String getType() {
		return type;
	}

	public void setType(String extensionType) {
		if (extensionType != null) {
			extensionType = extensionType.toLowerCase();
		}
		if (!"component".equals(extensionType)
				&& !"module".equals(extensionType)
				&& !"plugin".equals(extensionType)
				&& !"template".equals(extensionType)
				&& !"package".equals(extensionType)) {
			throw new IllegalArgumentException(
					"Extension type should be 'component', 'module', 'plugin', 'template' or 'package'");
		}
		this.type = extensionType;
	}

	public String getName() {
		return name;
	}

	public void setName(String componentName) {
		this.name = componentName;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String baseUrl) {
		this.url = baseUrl;
	}

	public String getAdminLogin() {
		return adminLogin;
	}

	public void setAdminLogin(String adminLogin) {
		this.adminLogin = adminLogin;
	}

	public String getAdminPassword() {
		return adminPassword;
	}

	public void setAdminPassword(String adminPassword) {
		this.adminPassword = adminPassword;
	}

	public String getFtpLogin() {
		return ftpLogin;
	}

	public void setFtpLogin(String ftpLogin) {
		this.ftpLogin = ftpLogin;
	}

	public String getFtpPassword() {
		return ftpPassword;
	}

	public void setFtpPassword(String ftpPassword) {
		this.ftpPassword = ftpPassword;
	}
	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}
	private String version="1.5";
	private String name = null;
	private String type = "component";
	private String url = null;
	private String adminLogin = null;
	private String adminPassword = null;
	private String ftpLogin = null;
	private String ftpPassword = null;
}
